package com.tm.shoot;

import javax.swing.text.Style;
import java.awt.image.BufferedImage;

public class Hero extends FlyingObject {
    private int life; //命
    private int doubleFire; //火力值
    private BufferedImage[] images = {}; //图片切换数组
    private int index = 0; //协助图片切换

    /**
     * 构造方法
     */
    public Hero() {
        image = Step1.hero0; //图片
        width = image.getWidth();   //宽
        height = image.getHeight(); //高
        x = 0; //x坐标:固定的值
        y = 0; //y坐标:固定的值
        life = 3; //命数为3
        doubleFire = 0; //火力值为0(单倍火力)
        images = new BufferedImage[]{Step1.hero0, Step1.hero1}; //两张图片切换
    }

    /**
     * 重写step()
     */
    public void step() {
        if (images.length > 0) {
            image = images[index++ / 10 % images.length];
        }
    }

    /**
     * 英雄机发射子弹
     */
    public Bullet[] shoot() {
        int xStep = this.width / 4; //1/4英雄机的宽
        int yStep = 20; //固定的值
        if (doubleFire > 0) { //双倍
            Bullet[] bs = new Bullet[3]; //四发子弹
            bs[0] = new Bullet((int) (this.x + 0.5 * xStep), this.y - yStep); //x:英雄机的x+0.5/4英雄机的宽 y:英雄机的y-20
            bs[1] = new Bullet((int) (this.x + 1.5 * xStep), this.y - yStep); //x:英雄机的x+1.5/4英雄机的宽 y:英雄机的y-20
            bs[2] = new Bullet((int) (this.x + 2.5 * xStep), this.y - yStep); //x:英雄机的x+2.5/4英雄机的宽 y:英雄机的y-20
            doubleFire -= 2; //发射一次四倍火力时，火力值减2
            return bs;
        } else { //单倍
            Bullet[] bs = new Bullet[1]; //一发子弹
            bs[0] = new Bullet((int) (this.x + 1.5 * xStep), this.y - yStep); //x:英雄机的x+2/4英雄机的宽 y:英雄机的y-20
            return bs;
        }
    }
    /**
     * 英雄机随着鼠标移动  x:鼠标的x坐标  y:鼠标的y坐标
     */
    public void moveTo(int x, int y) {
        this.x = x - this.width / 2;  //英雄机的x:鼠标的x-1/2英雄机的宽
        this.y = y - this.height / 2; //英雄机的y:鼠标的y-1/2英雄机的高
    }

    /**
     * 英雄机增火力
     */
    public void addDoubleFire() {
        doubleFire += 40; //火力值增40
    }

    /**
     * 增命
     */
    public void addLife() {
        life++; //命数增1
    }

    /**
     * 获取命
     */
    public int getLife() {
        return life; //返回命数
    }

    /**
     * 减命
     */
    public void subtractLife() {
        life--;
    }

    public void setDoubleFire(int doubleFire) {
        this.doubleFire = doubleFire;
    }

    /**
     * 重写outOfBounds()
     */
    public boolean outOfBounds() {
        return false; //永不越界
    }

    /**
     * 检测英雄机与敌人的碰撞 this:英雄机 other:敌人
     */
    public boolean hit(FlyingObject other) {
        int x1 = other.x - this.width / 2; //x1:敌人的x-1/2英雄机的宽
        int x2 = other.x + other.width + this.width / 2; //x2:敌人的x+敌人的宽+1/2英雄机的宽
        int y1 = other.y - this.height / 2; //y1:敌人的y-1/2英雄机的高
        int y2 = other.y + other.height + this.height / 2; //y2:敌人的y+敌人的高+1/2英雄机的高
        int x = this.x + this.width / 2;  //x:英雄机的x+1/2英雄机的宽
        int y = this.y + this.height / 2; //y:英雄机的y+1/2英雄机的高

        return x > x1 && x < x2 && y > y1 && y < y2; //x在x1和x2之间，并且，y在y1和y2之间，即为撞上了
    }
}

